package online.longzipeng.mywebdemo.commen.exception;

import online.longzipeng.mywebdemo.commen.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 通用异常处理器
 */
@RestControllerAdvice
public class ServiceExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(ServiceException.class)
	public Result handleRenException(ServiceException e) {
		return Result.generate(e.getCode(),e.getMsg());
	}

	/**
	 * 处理未知异常
	 */
	@ExceptionHandler(Exception.class)
	public Result handleException(Exception e) {
		logger.error(e.getMessage(), e);
		return Result.error();
	}
}
